package br.com.mtg.movethegarbage.screens;

import android.util.Log;
import br.com.mtg.movethegarbage.framework.Game;
import br.com.mtg.movethegarbage.framework.GameTouchEvent;
import br.com.mtg.movethegarbage.framework.Input;
import br.com.mtg.movethegarbage.framework.Screen;
import br.com.mtg.movethegarbage.game.Assets;
import br.com.mtg.movethegarbage.game.R;
import br.com.mtg.movethegarbage.graphics.Camera2D;
import br.com.mtg.movethegarbage.graphics.Sprite;
import br.com.mtg.movethegarbage.graphics.SpriteManager;
import br.com.mtg.movethegarbage.impl.Button;

public class MapScreen extends Screen
{
	Camera2D cam;
	int progress;
	int world;
	Button nextWorld, prevWorld, buttonBack;
	Button map1, map2, map3, map4;
	Sprite background;
	SpriteManager spriteManager;
	boolean tutorial;

	public MapScreen(Game _game)
	{
		super(_game);

		multiplier = game.getGraphics().getMultiplier();
		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		spriteManager = new SpriteManager(game.getGraphics(), 1);
		cam = new Camera2D(game.getGraphics());

		nextWorld = new Button(width - (40 * multiplier + 10), height / 2 - 40 * multiplier / 2, 40 * multiplier, 40 * multiplier);
		prevWorld = new Button(10, height / 2 - 40 * multiplier / 2, 40 * multiplier, 40 * multiplier);
		buttonBack = new Button(10, 10, 40 * multiplier, 40 * multiplier);
		map3 = new Button(width / 2 - (60 * multiplier + 10 * multiplier), height / 2 - (60 * multiplier + 10 * multiplier), 60 * multiplier, 60 * multiplier);
		map4 = new Button(width / 2 + 10 * multiplier, height / 2 - (60 * multiplier + 10 * multiplier), 60 * multiplier, 60 * multiplier);
		map1 = new Button(width / 2 - (60 * multiplier + 10 * multiplier), height / 2 + 10 * multiplier, 60 * multiplier, 60 * multiplier);
		map2 = new Button(width / 2 + 10 * multiplier, height / 2 + 10 * multiplier, 60 * multiplier, 60 * multiplier);

		nextWorld.setButtonSprite(Assets.buttonArrowWhite2);
		prevWorld.setButtonSprite(Assets.buttonArrowWhite1);
		buttonBack.setButtonSprite(Assets.buttonBack);

		try
		{
			progress = game.loadProgress();
		}
		catch (Exception e)
		{
			Log.e("MapScreen", "Error loading progress");
			progress = 1;
		}

		if (progress == 0)
		{
			tutorial = true;
			progress = 1;
		}
		else
		{
			tutorial = false;
		}	
		world = progress / 5;
		setScreenSprites();
	}

	private void setScreenSprites()
	{
		int maps = progress - world * 4;
		switch (world)
		{
			case 0:
				if (maps > 0)
					map1.setButtonSprite(Assets.buttonRed1);
				else
					map1.setButtonSprite(Assets.buttonRedLocked);
				if (maps > 1)
					map2.setButtonSprite(Assets.buttonRed2);
				else
					map2.setButtonSprite(Assets.buttonRedLocked);
				if (maps > 2)
					map3.setButtonSprite(Assets.buttonRed3);
				else
					map3.setButtonSprite(Assets.buttonRedLocked);
				if (maps > 3)
					map4.setButtonSprite(Assets.buttonRed4);
				else
					map4.setButtonSprite(Assets.buttonRedLocked);

				background = Assets.redScreen;
				break;
			case 1:
				if (maps > 0)
					map1.setButtonSprite(Assets.buttonBlue5);
				else
					map1.setButtonSprite(Assets.buttonBlueLocked);
				if (maps > 1)
					map2.setButtonSprite(Assets.buttonBlue6);
				else
					map2.setButtonSprite(Assets.buttonBlueLocked);
				if (maps > 2)
					map3.setButtonSprite(Assets.buttonBlue7);
				else
					map3.setButtonSprite(Assets.buttonBlueLocked);
				if (maps > 3)
					map4.setButtonSprite(Assets.buttonBlue8);
				else
					map4.setButtonSprite(Assets.buttonBlueLocked);

				background = Assets.blueScreen;
				break;
			case 2:
				if (maps > 0)
					map1.setButtonSprite(Assets.buttonYellow9);
				else
					map1.setButtonSprite(Assets.buttonYellowLocked);
				if (maps > 1)
					map2.setButtonSprite(Assets.buttonYellow10);
				else
					map2.setButtonSprite(Assets.buttonYellowLocked);
				if (maps > 2)
					map3.setButtonSprite(Assets.buttonYellow11);
				else
					map3.setButtonSprite(Assets.buttonYellowLocked);
				if (maps > 3)
					map4.setButtonSprite(Assets.buttonYellow12);
				else
					map4.setButtonSprite(Assets.buttonYellowLocked);

				background = Assets.yellowScreen;
				break;
			case 3:
				if (maps > 0)
					map1.setButtonSprite(Assets.buttonGreen13);
				else
					map1.setButtonSprite(Assets.buttonGreenLocked);
				if (maps > 1)
					map2.setButtonSprite(Assets.buttonGreen14);
				else
					map2.setButtonSprite(Assets.buttonGreenLocked);
				if (maps > 2)
					map3.setButtonSprite(Assets.buttonGreen15);
				else
					map3.setButtonSprite(Assets.buttonGreenLocked);
				if (maps > 3)
					map4.setButtonSprite(Assets.buttonGreen16);
				else
					map4.setButtonSprite(Assets.buttonGreenLocked);

				background = Assets.greenScreen;
				break;
			case 4:
				if (maps > 0)
					map1.setButtonSprite(Assets.buttonOrange17);
				else
					map1.setButtonSprite(Assets.buttonOrangeLocked);
				if (maps > 1)
					map2.setButtonSprite(Assets.buttonOrange18);
				else
					map2.setButtonSprite(Assets.buttonOrangeLocked);
				if (maps > 2)
					map3.setButtonSprite(Assets.buttonOrange19);
				else
					map3.setButtonSprite(Assets.buttonOrangeLocked);
				if (maps > 3)
					map4.setButtonSprite(Assets.buttonOrange20);
				else
					map4.setButtonSprite(Assets.buttonOrangeLocked);

				background = Assets.orangeScreen;
				break;
		}
	}

	@Override
	public void update(long delta)
	{
		Input input = game.getInput();
		int maps = progress - world * 4;

		synchronized (input)
		{
			for (GameTouchEvent e : input.getTouchEvents())
			{
				prevWorld.handleEvent(e, game);
				nextWorld.handleEvent(e, game);
				buttonBack.handleEvent(e, game);
				if (maps > 0)
					map1.handleEvent(e, game);
				if (maps > 1)
					map2.handleEvent(e, game);
				if (maps > 2)
					map3.handleEvent(e, game);
				if (maps > 3)
					map4.handleEvent(e, game);
			}
			input.getTouchEvents().clear();
			input.getKeyEvents().clear();
		}

		if (buttonBack.wasPressed() || input.wasBackPressed())
			game.setScreen(new MainMenuScreen(game));
		else if (prevWorld.wasPressed())
		{
			world = world == 0 ? 4 : world - 1;
			setScreenSprites();
		}
		else if (nextWorld.wasPressed())
		{
			world = world == 4 ? 0 : world + 1;
			setScreenSprites();
		}
		else if (maps > 0)
		{
			if (map1.wasPressed())
			{
				if (tutorial)
					game.setScreen(new TutorialScreen(game));
				else
					game.setScreen(new HintScreen(game, world * 4 + 1));
			}
			else if (maps > 1)
			{
				if (map2.wasPressed())
				{
					game.setScreen(new HintScreen(game, world * 4 + 2));
				}
				else if (maps > 2)
				{
					if (map3.wasPressed())
					{
						game.setScreen(new HintScreen(game, world * 4 + 3));
					}
					if (maps > 3)
					{
						if (map4.wasPressed())
						{
							game.setScreen(new HintScreen(game, world * 4 + 4));
						}
					}
				}
			}
		}
	}

	@Override
	public void present(long delta)
	{
		spriteManager.beginDraw(background.texture);
		spriteManager.drawSprite(0, 0, game.getGraphics().getWidth(), game.getGraphics().getHeight(), background);
		spriteManager.endDraw();

		Assets.font.setScale(1.0F * multiplier);
		String worldName;
		float stringWidth;
		switch (world)
		{
			case 0:
				worldName = game.getStringFromResource(R.string.plastic);
				stringWidth = Assets.font.getTextLength(worldName);
				Assets.font.printAt(worldName, width / 2 - stringWidth / 2, height - (Assets.font.getTextHeight() + 10));
				break;
			case 1:
				worldName = game.getStringFromResource(R.string.paper);
				stringWidth = Assets.font.getTextLength(worldName);
				Assets.font.printAt(worldName, width / 2 - stringWidth / 2, height - (Assets.font.getTextHeight() + 10));
				break;
			case 2:
				worldName = game.getStringFromResource(R.string.metal);
				stringWidth = Assets.font.getTextLength(worldName);
				Assets.font.printAt(worldName, width / 2 - stringWidth / 2, height - (Assets.font.getTextHeight() + 10));
				break;
			case 3:
				worldName = game.getStringFromResource(R.string.glass);
				stringWidth = Assets.font.getTextLength(worldName);
				Assets.font.printAt(worldName, width / 2 - stringWidth / 2, height - (Assets.font.getTextHeight() + 10));
				break;
			case 4:
				worldName = game.getStringFromResource(R.string.battery);
				stringWidth = Assets.font.getTextLength(worldName);
				Assets.font.printAt(worldName, width / 2 - stringWidth / 2, height - (Assets.font.getTextHeight() + 10));
				break;
		}

		prevWorld.draw(game.getGraphics());
		nextWorld.draw(game.getGraphics());
		buttonBack.draw(game.getGraphics());

		map1.draw(game.getGraphics());
		map2.draw(game.getGraphics());
		map3.draw(game.getGraphics());
		map4.draw(game.getGraphics());
	}

	@Override
	public void pause()
	{
	}

	@Override
	public void resume()
	{
		cam.setViewAndMatrices();

		game.getGraphics().initialize();

		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		multiplier = game.getGraphics().getMultiplier();
		if (!Assets.music1.isPlaying())
			game.getAudio().playMusic(Assets.music1);
	}

	@Override
	public void dispose()
	{
		// TODO Auto-generated method stub

	}

}
